﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tp.Dto;
using Tp.Dto.Allgemein;
using Tp.Dto.Enums;
using Tp.Dto.Stammdaten;
using Tp.Models;

namespace Tp.Services.DataLayer
{
    internal class PersonenDataLayer : BaseSqlDataLayer
    {
        public virtual PersonDto LoadPerson(int personID)
        {
            string sql = "dbo.GetPerson @personID";
            using (IDataReader reader = this.GetDataReader(sql, new SqlParameter("@personID", personID)))
            {
                return (reader.Read() ? LoadPersonFromDataReader(reader) : null);
            }
        }

        protected virtual PersonDto LoadPersonFromDataReader(IDataReader reader)
        {
            ClientEnums.Sex sex;
            if (this.GetByte(reader, "Sex") == 0)
            {
                sex = ClientEnums.Sex.Man;
            }
            else
            {
                sex = ClientEnums.Sex.Woman;
            }

            AddressTypeDto adrType = new AddressTypeDto(
                  this.GetInt32(reader, "AdressenTypeID"),
                  this.GetString(reader, "AdressenType")
                  );

            AddressDto adr = new AddressDto(
                    this.GetInt32(reader, "AdressenID"),
                    adrType,
                    this.GetString(reader, "Plz"),
                    this.GetString(reader, "Ort"),
                    this.GetString(reader, "Strasse"),
                    this.GetString(reader, "Hausnummer"),
                    this.GetString(reader, "Vermerk"),
                    this.GetByte(reader, "Zone"),
                    this.GetDecimal(reader, "Entfernung"),
                    this.GetString(reader, "Land")
                    );

            TelefonDto tel = new TelefonDto(
                        this.GetInt32(reader, "TelKontaktID"),
                        this.GetString(reader, "TelKontaktPrefix"),
                        this.GetString(reader, "TelKontaktWert"),
                        this.GetString(reader, "TElKontaktSuffix")
                        );

            TelefonDto mob = new TelefonDto(
                  this.GetInt32(reader, "MobKontaktID"),
                  this.GetString(reader, "MobKontaktPrefix"),
                  this.GetString(reader, "MobKontaktWert"),
                  this.GetString(reader, "MobKontaktSuffix")
                  );

            TelefonDto fax = new TelefonDto(
                  this.GetInt32(reader, "FaxKontaktID"),
                  this.GetString(reader, "FaxKontaktPrefix"),
                  this.GetString(reader, "FaxKontaktWert"),
                  this.GetString(reader, "FaxKontaktSuffix")
                  );

            TitleDto titel1 = new TitleDto(
                this.GetInt32(reader, "Titel1ID"),
                  this.GetString(reader, "Titel1")
                  );

            TitleDto titel2 = new TitleDto(
                this.GetInt32(reader, "Titel2ID"),
                  this.GetString(reader, "Titel2")
                  );

            TitleDto titel3 = new TitleDto(
                this.GetInt32(reader, "Titel3ID"),
                  this.GetString(reader, "Titel3")
                  );

            PersonDto pers = new PersonDto(
                this.GetInt32(reader, "PersonID"),
                this.GetString(reader, "Vorname"),
                this.GetString(reader, "Nachname"),
                sex,
                titel1,
                titel2,
                titel3,
                adr,
                mob,
                tel,
                fax,
                this.GetString(reader, "Notiz"),
                this.GetBoolean(reader, "Mitarbeiter"),
                this.GetString(reader, "Initials"),
                this.GetString(reader, "MailKontaktWert")
                );


            return pers;
        }
    }
}
